function addProduct(id) {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if (document.getElementById("div_cart_full"))
                document.getElementById("b_cart").innerHTML = 'Aveti ' + xmlhttp.responseText + ' produse in cos';
            else
                document.getElementById("div_cart").innerHTML = '<br /><br /><div id="div_cart_full"><img src="img/site/cart_full.gif" alt="?" /> <b id="b_cart">Aveti ' + xmlhttp.responseText + ' produse in cos</b></div><hr style="margin: 5px 10% 5px 10%" /><div><a href="cart" class="link"><b>Vezi cos</b></a></div>';
        }
    }
    xmlhttp.open("GET", "cart?action=add&id=" + id,true);
    xmlhttp.send();
}

$(document).ready(function() {
    $("a#myphoto").fancybox( {
        'titlePosition' : 'inside',
        'overlayShow' : false,
        'transitionIn' : 'elastic',
        'transitionOut' : 'elastic'
    });
});

// //////////////////// validation ////
// ////////////////////////////////////////////////////////////////////
function CheckQuantity() {
    try {
        var quantity = document.getElementById("quantity").value;

        if (quantity == '')
            throw 'Introduceti cantitatea';
        else if (isNaN(quantity))
            throw 'Introduceti numai cifre';
        else if (quantity <= 0)
            throw 'Cantitatea trebuie sa fie pozitiva';

        var label = document.getElementById("lbError");
        if (label)
            document.getElementById("div_quantity").removeChild(label);

        return true;

    } catch (err) {

        var label = document.getElementById("lbError");
        if (label)
            label.innerHTML = err;
        else {
            label = document.createElement('label');
            label.setAttribute('id', 'lbError');
            label.setAttribute('for', 'quantity');
            label.setAttribute('generated', 'true');
            label.setAttribute('class', 'error');

            label.innerHTML = err;
            document.getElementById("div_quantity").appendChild(label);
        }
    }
}

function redirect(url) {

    var quantity = document.getElementById("quantity").value;

    if (CheckQuantity())
        window.location = url + quantity;
}
